<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of TokyoTyrant
 *
 * @author ananda
 */
class Connection_TtCache extends Connection_Base
{
    /**
     * 连接驱动创建及初始化
     */
    public function  init()
    {
        $host    = $this->conf['host'];
        $port    = isset($this->conf['port'])    ? $this->conf['port']    : null;
        $options = isset($this->conf['options']) ? $this->conf['options'] : null;

        //创建对象
        $this->driver = new TokyoTyrant;
        if (!$this->driver->connect($host, $port, $options)) {
            throw new Exception_Kernel('TokyoTyrant connect fail', 10);
        }
    }

    public function read($key)
    {
        if (!is_array($key)) {
            $key = array($key);
        }

        $result = $this->driver->get($key);
        if ($result === null) {
            return new Error('TokyoTyrant key not exists');
        }
        return $this->buildResult('Data_Collection_TtCache', $result);
    }

    public function readOne($key)
    {
        $result = $this->driver->get($key);
        if ($result === null) {
            return new Error("TokyoTyrant key [$key] not exists", 1101);
        }
        $result = self::decode($result);
        return is_array($result) ? $this->buildResult('Data_Record_TtCache', $result) : $result;
    }

    public function create($key, $value)
    {
        try {
            if (!$this->driver->putKeep($key, self::encode($value))) {
                return new Error('Create value fail');
            }
        } catch (TokyoTyrantException $e) {
            throw new Exception($e->getMessage(), 1301);
        }
        return true;
    }

    public function update($key, $value, $asyn = false)
    {
        if ($asyn) {
            $result = $this->driver->putNr($key, self::encode($value));
        } else {
            $result = $this->driver->put($key, self::encode($value));
        }
        if (!$result) {
            return new Error('Update value fail');
        }
        return true;
    }

    public function delete($key)
    {
        if (!$this->driver->out($key)) {
            return new Error('Delete fail');
        }
        return ;
    }

    public static function encode($value)
    {
        return serialize($value);
    }

    public static function decode($value)
    {
        return is_string($value) ? unserialize($value) : $value;
    }

    public function rowCount()
    {
        return $this->driver->num();
    }

    public function searchKey($key, $limit)
    {
        $result = $this->driver->fwmKeys($key, $limit);
        if (!$result) {
            if (is_array($result)) {
                return new Error('key is not exists');
            } else {
                return new Error('Unknown error');
            }
        }
        return $result;
    }

    public function getIncrement($key, $value, $type = TokyoTyrant::RDBREC_INT)
    {
        $result = $this->driver->add($key, $value, $type);
        if (!$result) {
            return new Error('Increment error');
        }
        return $result;
    }

    public function close()
    {
        unset($this->driver);
    }


}
?>